<?php

namespace app\admin\server\group;

use app\admin\model\group\GroupGoodsModel;
use app\admin\model\group\GroupOrderModel;
use app\admin\model\user\UserModel;
use app\admin\ServerBase;
use app\api\model\order\OrderMusicDetailApiModel;
use app\api\model\user\UserWriterWaterApiModel;
use app\logic\single\GoodsSingle;
use think\facade\Db;
use think\Request;

class GroupOrderServer extends ServerBase
{

    /**
     * @var GroupOrderModel
     */
    private $orderModel;

    function __construct(Request $request)
    {
        $this->request = $request;
        $this->param = $request->param();
        $this->model = new GroupOrderModel();
        $this->userWriterWaterModel = new UserWriterWaterApiModel();
    }
    public function getDetail(){
        $info = $this->model->getDetail($this->request->param('id'));
        $this->getMessageCode('adminMessage.success.select', 2);
        return $info;
    }
    function export(){
        [$list,$count] = $this->model->getList($this->request,[],'*',1,999999);
        $header=[
            '编号',
            '订单号',
            '金额',
            '买家昵称',
            '买家手机号',
            '状态',
            '支付方式',
            '创建时间',
            '支付时间',
        ];
        $buyer_ids = array_column($list,'buyer_id');
        $buyers = UserModel::getInfosByIds($buyer_ids,'nickname,mobile');
        $data=[];
        $status_arr = $this->model::getStatusArr();
        $pay_arr = $this->model::getPayArr();
        foreach ($list as $val){
            $data[]=[
                $val['id'],
                $val['order_no'],
                $val['order_price'],
                $buyers[$val['buyer_id']]['nickname']??'',
                $buyers[$val['buyer_id']]['mobile']??'',
                $status_arr[$val['status']]??'',
                $pay_arr[$val['pay_type']]??'',
                $val['create_time'],
                $val['pay_time'],
            ];
        }
        $this->excelExport('音乐订单',$header,$data);

    }

    function audit(){
        $order_id = $this->request->param('order_id')??'';
        if(empty($order_id)){
            $this->getMessageCode('adminMessage.error.select',1);
            return false;
        }
        $order = $this->model->whereIn('id',$order_id)->select();
        Db::startTrans();
        try {
            foreach ($order as &$val){
                if($val['writer_can_see'] == -1){
                    //修改状态
                    GroupOrderModel::where('id',$val['id'])
                        ->update(['writer_can_see'=>1]);
                    $inc_goods_sale[]=[
                        'goods_id'=>$val['music_goods_id'],
                        'type'=>$val['music_goods_type'],
                    ];
                    $writer_water_insert[]=[
                        'user_id'=>$val['sell_id'],
                        'type' => 2,
                        'price'=>$val['writer_price'],
                        'source'=>30,
                        'order_id'=>$order_id,
                        'order_detail_id'=>$val['id'],
                        'remark'=>'出售拼团商品获得佣金'.$val['writer_price'] . '元',
                        'music_type'=>$val['music_goods_type'],
                    ];
                }
            }
            if(!empty($inc_goods_sale)){
                GoodsSingle::getInstance()->IncSales($inc_goods_sale);
            }
            if(!empty($writer_water_insert)){
                $this->userWriterWaterModel->saveAll($writer_water_insert);
            }
            Db::commit();
            return true;
        }catch (\Exception $exception){
            Db::rollback();
            $this->ERROR=$exception->getMessage();
            return false;
        }
    }
}